//
//  SettingViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/14.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "SettingViewController.h"
#import "LoginStatuesModel.h"
#import "SettingTableViewCell.h"
#import "OutLoginTableViewCell.h"
#import "SetPWDViewController.h"
#import "SettingModel.h"
#import "AddGroupViewController.h"
#import "GroupGetModel.h"
#import "SafeFoodManagerDemo-Bridging-Header.h"
#import "SafeFoodManagerDemo-Swift.h"
@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView*SetTableView;

@property(nonatomic,strong)NSMutableArray *dataArr;



@end

@implementation SettingViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.title=@"个人信息";

 
  [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kColorRGBA(0x2E4361, 1)] forBarMetrics:UIBarMetricsDefault];
    
    [self.view addSubview:self.SetTableView];
    
    [self addNavigationItemWithImageNames:@[@"返回白色"] Titles:@[@"返回"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
    // Do any additional setup after loading the view.
}

- (void)backBtnClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSMutableArray*)dataArr
{
    if (!_dataArr) {
        
        _dataArr=[NSMutableArray array];
        
        NSMutableArray *arr1=[NSMutableArray array];
        
        if ([self getUserType]==2||[self getUserType]==4) {
            
            //商户或者检查员
            
            for (NSInteger i=0; i<2; i++) {
                
                SettingModel *model=[SettingModel new];
                
                switch (i) {
                    case 0:
                    {
                        
                        model.imageName=@"shopGroup";
                        
                        model.name=@"商户分组";
                        
                        model.detial=@"";
                        
                    }
                        break;
                        
                    default:
                    {
                        model.imageName=@"CheckGroup";
                        
                        model.name=@"检查员分组";
                        
                        model.detial=@"";
                        
                    }
                        break;
                }
                
                [arr1 addObject:model];
                
            }
            
            [_dataArr addObject:arr1];
            
            
        }
        
        
        
        
        NSMutableArray *arr2=[NSMutableArray array];
        
        for (NSInteger i=0; i<4; i++) {
            
            SettingModel *model=[SettingModel new];
            
            switch (i) {
                case 0:
                {
                    model.imageName=@"usercpwd";
                    
                    model.name=@"修改密码";
                    
                    model.detial=@"";
                    
                }
                    break;
                    
                case 1:
                {
                    model.imageName=@"usercpwd";
                    
                    model.name=@"我的学习";
                    
                    model.detial=@"";
                    
                }
                    break;
                    
                case 2:
                {
                    model.imageName=@"usercpwd";
                    
                    model.name=@"我的收藏";
                    
                    model.detial=@"";
                    
                }
                    break;
                    
                default:
                {
                    model.imageName=@"version";
                    
                    model.name=@"版本号";
                    
                    model.detial=kApplication.appVersion;
                    
                }
                    break;
            }
            
            [arr2 addObject:model];
            
        }
        
        [_dataArr addObject:arr2];
        
        
        NSMutableArray *arr3=[NSMutableArray array];
        
        SettingModel *model=[SettingModel new];
        
        model.imageName=@"";
        
        model.name=@"退出登录";
        
        model.detial=@"";
        
        [arr3 addObject:model];
        
        [_dataArr addObject:arr3];
 
        }

    
    return _dataArr;
}

-(TB_TableView*)SetTableView
{
    
    if (!_SetTableView) {
        
        _SetTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        
        _SetTableView.delegate=self;
        
        _SetTableView.dataSource=self;
        
        [_SetTableView registerNib:[UINib nibWithNibName:@"SettingTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        [_SetTableView registerNib:[UINib nibWithNibName:@"OutLoginTableViewCell" bundle:nil] forCellReuseIdentifier:@"OutCell"];
        
        _SetTableView.tableFooterView=[[UIView alloc]init];
        
    }
    
    return _SetTableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSArray *arr=self.dataArr[section];
    
    return arr.count;
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    
    return self.dataArr.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr=self.dataArr[indexPath.section];
    
    SettingModel *model=arr[indexPath.row];
    
    if (indexPath.section<self.dataArr.count-1) {
        
        SettingTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        cell.TipLabel.text=model.name;
        
       
        cell.IcoImageView.image=[UIImage imageNamed:model.imageName];
        
        if (model.detial.length!=0) {
            
            cell.DetialLabel.text=model.detial;
            cell.accessoryType=UITableViewCellAccessoryNone;
        }else{
            
            cell.DetialLabel.text=@"";
            
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        }
        
        return cell;
        
    }else{
        
        OutLoginTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"OutCell" forIndexPath:indexPath];
        
        cell.title=model.name;
        
        cell.Titlecolor=TBColorOutLoginbtnbg;
        
        cell.bgColor=TBColorWhite;
        
        cell.CLblock=^()
        {
            [self TipOutLogin];
        };
        
        return cell;
        
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50*SCREEN_WIDTH/320.0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
 
    return 15 *SCREEN_WIDTH/320.0 ;
    
}


-(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor=TBColorTableViewBg;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSArray *arr=self.dataArr[indexPath.section];
  
    SettingModel *model=arr[indexPath.row];
    
        if ([model.name isEqualToString:@"修改密码"]) {
            
            [self changePWD];
            
        }else if ([model.name isEqualToString:@"商户分组"])
        {
            GroupGetModel *list=[GroupGetModel new];
            
            AddGroupViewController *storeGroup=[AddGroupViewController new];
   
            storeGroup.title=model.name;

            storeGroup.isEdit=YES;
                
            storeGroup.isdeleMore=YES;
                
            list.staffId=[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]];
    
            list.groupType=@"1";
            
            storeGroup.Reqmodel=list;
            
            storeGroup.searchName=@"输入分组名称";
            storeGroup.AddBtnTitle=@"新增分组";
      
            [self.navigationController pushViewController:storeGroup animated:YES];
            
        }else if ([model.name isEqualToString:@"检查员分组"])
        {
             GroupGetModel *list=[GroupGetModel new];
            
            AddGroupViewController *CheckGroup=[AddGroupViewController new];
            CheckGroup.title=model.name;
            
            CheckGroup.isEdit=YES;
            
            CheckGroup.isdeleMore=YES;
            
            list.staffId=[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]];
            
            list.groupType=@"2";
            
            CheckGroup.Reqmodel=list;
            
            CheckGroup.searchName=@"输入分组名称";
            CheckGroup.AddBtnTitle=@"新增分组";
            
            [self.navigationController pushViewController:CheckGroup animated:YES];
            
        }else if ([model.name isEqualToString:@"我的学习"]){
            
            LearnTableViewController *vc = [LearnTableViewController new];
            
            [self.navigationController pushViewController:vc animated:YES];
            
            
            
        }else if ([model.name isEqualToString:@"我的收藏"]){
            
            CollectionMainViewController *vc = [CollectionMainViewController new];
            
            [self.navigationController pushViewController:vc animated:YES];
        }
 
}


-(void)TipOutLogin
{
    [UIAlertView bk_showAlertViewWithTitle:@"确定要退出登录吗?" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        
        if (buttonIndex==1) {
            
            [self OutLogin];
        }
        
    }];
    
    
}


-(void)changePWD{
    
    SetPWDViewController *SetPWD=[SetPWDViewController new];
    
    [self.navigationController pushViewController:SetPWD animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
